Skip to content

fix(quizzes): add configurable timeout to AI service fetch - #85

Merged
DeFiVC merged 1 commit into
ChainLearnOfficial:mainfrom
AbelOsaretin:fix/ai-client-timeout
Jul 23, 2026
Merged

fix(quizzes): add configurable timeout to AI service fetch#85
DeFiVC merged 1 commit into
ChainLearnOfficial:mainfrom
AbelOsaretin:fix/ai-client-timeout

Conversation

@AbelOsaretin

@AbelOsaretin AbelOsaretin commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Closes #74

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Summary

The fetch call to the AI service in ai-client.ts had no AbortController or timeout configuration. If the AI service hangs, the request blocks indefinitely, holding open sockets and event loop references. Under AI service degradation, this can exhaust available sockets.

This PR adds an AbortController with a configurable timeout (default 30s) via the new AI_TIMEOUT_MS environment variable. Hung connections are now cleaned up and a meaningful error is thrown.

Motivation / Context

Fixes #74 — under AI service degradation, hung fetch connections keep Node.js event loop references and sockets open, which can exhaust resources. The existing fallback in quizService.generateQuiz catches errors and falls back to placeholder questions, but couldn't handle hung connections.

Detailed Changes

src/config/index.ts:

  • Added AI_TIMEOUT_MS env var with z.coerce.number().default(30_000) — configurable per environment

src/modules/quizzes/ai-client.ts:

  • Wrapped fetch call with AbortController using config.AI_TIMEOUT_MS
  • Added signal: controller.signal to the fetch options
  • Added catch block to detect AbortError and throw a descriptive timeout error
  • Added finally block to clear the timeout timer on success or failure

Testing

  • Lint: 0 errors (75 pre-existing warnings)
  • Typecheck: passes (pre-existing async-lock module error unrelated)
  • Unit tests: 80/80 passing (8 failed suites are pre-existing infra issues — e2e tests requiring Redis/DB)

The fetch call to the AI service in ai-client.ts had no AbortController
or timeout configuration. If the AI service hangs, the request blocks
indefinitely, holding open sockets and event loop references. Under AI
service degradation this can exhaust available sockets.

Add AbortController with a configurable timeout (default 30s) via the
new AI_TIMEOUT_MS environment variable. The timeout error is caught,
logged, and re-thrown so the existing fallback in quizService.generateQuiz
continues to work.

Closes ChainLearnOfficial#74

@DeFiVC DeFiVC left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@DeFiVC DeFiVC left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Clean AbortController pattern, proper cleanup in finally, and the timeout error integrates with the existing fallback in quizService.generateQuiz. CI is green, description is thorough. Approved.

@DeFiVC
DeFiVC merged commit 526d17b into ChainLearnOfficial:main Jul 23, 2026
2 checks passed
@grantfox-oss grantfox-oss Bot mentioned this pull request Jul 23, 2026
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AI client fetch has no timeout

2 participants